package com.example.restatr.Atcivity.LawyerConsultation;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.restatr.Adapter.CardAdapter;
import com.example.restatr.Atcivity.Main.SearchAt;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.Atcivity.LawyerConsultation.Bean.LawyerInfoBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LawyerConsultationAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ImageView searchIcon;
    private Banner rbt;
    private ViewPager card;
    private List<CardBean> cardBeans1 = new ArrayList<>();
    private List<CardBean> cardBeans2 = new ArrayList<>();
    private List<GridView> gridViews = new ArrayList<>();
    private CardView zsq1;
    private CardView zsq2;
    private TextView more;
    private RecyclerView rv;
    private List<LawyerInfoBean.RowsBean> rowsBeans = new ArrayList<>();
    private LinearLayout loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_lawyer_consultation_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        searchIcon = findViewById( R.id.searchIcon );
        rbt = findViewById( R.id.banner );
        card = findViewById( R.id.card );
        zsq1 = findViewById( R.id.zsq1 );
        zsq2 = findViewById( R.id.zsq2 );
        more = findViewById( R.id.more );
        rv = findViewById( R.id.rv );
        loading = findViewById( R.id.loading );
        Tools.SetBarTitle( toolbar, barTitle, "法律咨询", search, this, true );
        Menu menu = toolbar.getMenu();
        menu.getItem( 0 ).setTitle( "我的咨询" );
        menu.removeItem( R.id.item2 );
        searchIcon.setVisibility( View.VISIBLE );
        searchIcon.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( LawyerConsultationAt.this, SearchAt.class );
                intent.putExtra( "intent", "法律咨询" );
                startActivity( intent );
            }
        } );
        toolbar.setOnMenuItemClickListener( new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity( new Intent( LawyerConsultationAt.this, MyLegalAdviceAt.class ) );
                return true;
            }
        } );
        Tools.SetBanner( "/prod-api/api/lawyer-consultation/ad-banner/list", MainActivity.sp.getString( "token", "" ), "data", "imgUrl",
                rbt, this );
        card.addOnPageChangeListener( new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0) {
                    zsq1.setCardBackgroundColor( Color.parseColor( "#FD4D5D" ) );
                    zsq2.setCardBackgroundColor( 0x9FBFBBBB );
                } else if (position == 1) {
                    zsq2.setCardBackgroundColor( Color.parseColor( "#FD4D5D" ) );
                    zsq1.setCardBackgroundColor( 0x9FBFBBBB );
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        } );

        more.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( LawyerConsultationAt.this, SearchAt.class );
                intent.putExtra( "intent", "法律咨询" );
                startActivity( intent );
            }
        } );
        GetCard();
        GetLawyerInfo();
    }

    private void GetCard() {
        new Thread( () -> {
            try {
                String token = Tools.Get( "/prod-api/api/lawyer-consultation/legal-expertise/list", MainActivity.sp.getString( "token", "" ) );
                JSONObject jx = new JSONObject( token );
                if (jx.getInt( "code" ) == 200) {
                    JSONArray rows = jx.getJSONArray( "rows" );
                    for (int i = 0; i < rows.length(); i++) {
                        JSONObject data = rows.getJSONObject( i );
                        if (i <= 7) {
                            cardBeans1.add( new CardBean( data.getString( "imgUrl" ), data.getString( "name" ), data.getString( "id" ) ) );
                        } else {
                            cardBeans2.add( new CardBean( data.getString( "imgUrl" ), data.getString( "name" ), data.getString( "id" ) ) );
                        }
                    }
                    runOnUiThread( () -> {
                        GridView gridView1 = new GridView( this );
                        GridView gridView2 = new GridView( this );
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
                        gridView1.setLayoutParams( layoutParams );
                        gridView1.setNumColumns( 4 );
                        gridView1.setAdapter( new CardAdapter( "url", this, cardBeans1 ) );
                        gridView2.setLayoutParams( layoutParams );
                        gridView2.setNumColumns( 4 );
                        gridView2.setAdapter( new CardAdapter( "url", this, cardBeans2 ) );
                        gridView1.setOnItemClickListener( new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent( LawyerConsultationAt.this, LawyerListAt.class );
                                intent.putExtra( "data", new Gson().toJson( cardBeans1.get( position ) ) );
                                startActivity( intent );
                            }
                        } );
                        gridView2.setOnItemClickListener( new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent( LawyerConsultationAt.this, LawyerListAt.class );
                                intent.putExtra( "data", new Gson().toJson( cardBeans2.get( position ) ) );
                                startActivity( intent );
                            }
                        } );
                        Collections.addAll( gridViews, gridView1, gridView2 );
                        card.setAdapter( new cardPagerAdapter() );
                    } );
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } ).start();
    }

    private void GetLawyerInfo() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/lawyer-consultation/lawyer/list-top10", MainActivity.sp.getString( "token", "" ) );
            LawyerInfoBean lawyerInfoBean = new Gson().fromJson( token, LawyerInfoBean.class );
            rowsBeans.addAll( lawyerInfoBean.getData() );
            runOnUiThread( () -> {
                if (rowsBeans.size() > 0) {
                    loading.setVisibility( View.GONE );
                    rv.setAdapter( new listAdapter() );
                }
            } );

        } ).start();
    }

    class cardPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return gridViews.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView( gridViews.get( position ) );
            return gridViews.get( position );
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView( gridViews.get( position ) );
        }
    }

    class listAdapter extends RecyclerView.Adapter<listAdapter.listViewHolder> {

        @NonNull
        @Override
        public listViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.lawyer_item, parent, false );
            return new listViewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull listViewHolder holder, int position) {
            Glide.with( LawyerConsultationAt.this )
                    .load( Tools.ip + rowsBeans.get( position ).getAvatarUrl() )
                    .transform( new CircleCrop() )
                    .into( holder.head );
            holder.name.setText( rowsBeans.get( position ).getName() );
            holder.workStartAt.setText( "从业时间" + rowsBeans.get( position ).getWorkStartAt() );
            holder.serviceTimes.setText( rowsBeans.get( position ).getServiceTimes() + "人咨询" );
            holder.favorableRate.setText( "好评率" + rowsBeans.get( position ).getFavorableRate() + "%" );
            holder.legalExpertiseName.setText( rowsBeans.get( position ).getLegalExpertiseName() );
            holder.cardBtn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( LawyerConsultationAt.this, LawyerInfoAt.class );
                    intent.putExtra( "data", new Gson().toJson( rowsBeans.get( position ) ) );
                    startActivity( intent );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class listViewHolder extends RecyclerView.ViewHolder {
            private ImageView head;
            private TextView name;
            private TextView workStartAt;
            private TextView serviceTimes;
            private TextView favorableRate;
            private TextView legalExpertiseName;
            private CardView cardBtn;

            public listViewHolder(@NonNull View itemView) {
                super( itemView );
                head = itemView.findViewById( R.id.head );
                name = itemView.findViewById( R.id.name );
                workStartAt = itemView.findViewById( R.id.workStartAt );
                serviceTimes = itemView.findViewById( R.id.serviceTimes );
                favorableRate = itemView.findViewById( R.id.favorableRate );
                legalExpertiseName = itemView.findViewById( R.id.legalExpertiseName );
                cardBtn = itemView.findViewById( R.id.cardBtn );
            }
        }
    }


}